#include "global.h"
#include "mass_mal.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "delay.h"
#include "led.h"
#include "ili9320.h"
#include "display.h"
#include "rte_rtc.h"
#include "keyboard.h"
#include "touch.h"
#include "usart.h"
#include "rs232_interface.h"
#include "i2c.h"



#ifdef STM32F10X_HD

void USB_Init(void);

/***************************************************
 *	Initialize hardware
 **************************************************/
void SystemStartup(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    delay_init(72);

    RCC_APB2PeriphClockCmd(0
                           | RCC_APB2Periph_AFIO
                           | RCC_APB2Periph_GPIOA
                           | RCC_APB2Periph_GPIOB
                           | RCC_APB2Periph_GPIOC
                           | RCC_APB2Periph_GPIOD
                           | RCC_APB2Periph_GPIOE
                           | RCC_APB2Periph_GPIOF
                           | RCC_APB2Periph_GPIOG
                           , ENABLE);

    SystemCoreClockUpdate();
//    if (SysTick_Config(SystemCoreClock / 1000))
//    {
//        led_on(LED_0);
//        while (1) { }
//    }


    //    PIN_INPUT_PU();
    //    PIN_SET_MODE(LIMIT_X_PORT, LIMIT_X_PIN);
    //    PIN_SET_MODE(LIMIT_Y_PORT, LIMIT_Y_PIN);
    //    PIN_SET_MODE(LIMIT_Z_PORT, LIMIT_Z_PIN);

    I2C_Config();
    LCD_Configuration();
    LCD_Initialization();
    LCD_Clear(Grey);
    LCD_BackLight(1);

    win_showMsgWin();
    delay_ms(50);
    if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == SUCCESS)
        scr_puts("   ---- CNC MM -----");
    else
        showCriticalStatus("HSE FAIL", 0);
    MAL_Init(0);
    LED_Init();
    kbd_init();
    Usart_Init(115200);


    rtc_init();

    DBG_TRACE("Initializing");
    stepm_init();
    DBG_TRACE("stepm initialized");

//    Touch_init();                   /*触摸屏初始化*/
    DBG_TRACE("Touch initialized");



#if (USE_USB == 1)
    // USB configure
    /* USB_DISCONNECT_PIN used as USB pull-up */

    GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_Init(USB_DISCONNECT_PORT, &GPIO_InitStructure);

    RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USB_Init();
#endif
}




#if (USE_USB == 1)
void USB_HP_CAN1_TX_IRQHandler(void)
{
    CTR_HP();
}
void USB_LP_CAN1_RX0_IRQHandler(void)
{
    USB_Istr();
}
#endif


void SPI2_IRQHandler(void)
{
    extrudT_irq();
}


#endif
